package com.ggh.jinjilive.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.databinding.ActivityLoginBinding;
import com.ggh.jinjilive.mobile.UserMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.TotalPageActivity;
import com.ggh.jinjilive.ui.mine.address.TheGoodsActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.VUtils;
import com.ggh.jinjilive.utils.loginIMUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.tencent.imsdk.v2.V2TIMCallback;


public class LoginActivity extends BaseActivity<ActivityLoginBinding>{

    private LoginViewModel mLoginViewModel;

    private boolean wherever;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mLoginViewModel = new ViewModelProvider(this,viewModelFactory).get(LoginViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        wherever = getIntent().getBooleanExtra("wherever",false);
        if(wherever){
            ActivityUtils.finishAllActivitiesExceptNewest();

        }
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;

    }


    @Override
    public void initView() {
        super.initView();
        //绑定
        binding.setMLoginViewModel(mLoginViewModel);
        //下移
        binding.llPlace.setPadding(0, SizeUtils.dp2px(50),0,0);
        //号码下移
        binding.llNumber.setPadding( SizeUtils.dp2px(20), SizeUtils.dp2px(70), SizeUtils.dp2px(20),0);
        //监听
        VUtils.doubleAddTextChangedListener(binding.etMobile,binding.etPassword,binding.loginButton);
    }

    @Override
    public void initData() {
        super.initData();
        //获取手机号
        String Mobile = MerchantSession.getInstance().getMobile();
        if(!TextUtils.isEmpty(Mobile)){
            mLoginViewModel.mobile.set(Mobile);
        }
        //获取密码
        String Password = MerchantSession.getInstance().getPassword();
        if(!TextUtils.isEmpty(Password)){
            mLoginViewModel.password.set(Password);
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        binding.loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
                mLoginViewModel.login();
            }
        });

        mLoginViewModel.userMobileMutableLiveData.observe(this, new Observer<UserMobile>() {
            @Override
            public void onChanged(UserMobile userMobile) {
                cancelDialog();
                if(null != userMobile){
                    String userSig = loginIMUtils.genTestUserSig(userMobile.getUser_id());
                    loginIMUtils.loginIM(userMobile.getUser_id(), userSig, new V2TIMCallback() {
                        @Override
                        public void onSuccess() {
                            LogUtils.d("IM登录成功");
                            MerchantSession.getInstance().setUser(userMobile);
                            Intent  intent =new Intent(mContext, TotalPageActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);//启动Activity
                            finish();
                        }

                        @Override
                        public void onError(int code, String desc) {
                            LogUtils.d(code + "IM登录失败" + desc);
                            //标记登录
                            MerchantSession.getInstance().setIsLogin(false);
                        }
                    });
                }else {

                }
            }
        });
    }

}
